package action;

import Bean.UserBean;
import Bean.UserDataBean;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 获取用户数据操作
 */
public class GetUserDataAction extends ActionSupport {

//    private String name;

    public String getUserData() throws Exception {

        HttpServletRequest request = ServletActionContext.getRequest();
        request.setCharacterEncoding("utf-8");
        ServletInputStream inputStream = request.getInputStream();

        StringBuilder stringBuilder = new StringBuilder();

        int len = 0;
        byte[] bytesBuf = new byte[1024];
        while ((len=inputStream.read(bytesBuf)) != -1){
            stringBuilder.append(new String(bytesBuf, 0, len));
        }

        String phone = stringBuilder.toString();

        System.out.println("根据手机号获取用户数据 ==> "+phone);

        //1.导入驱动
        //2.加载驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //3.java连接数据库（需要：用户名、密码、端口号、url）   java------>mysql 建桥
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/wu_db?useSSL=false&serverTimezone=UTC", "root", "123456");
//        //4.创建一个人：Sql
//        String sql="SELECT * FROM user_tb WHERE telephone = '"+phone+"'";
//        //5.创建一辆车：Statement
//        Statement statement = conn.createStatement();
//        //6.人上车，发车，走桥。
//        ResultSet resultSet = statement.executeQuery(sql);
//
//        while (resultSet.next()) {
//            name = resultSet.getString("name");
//            System.out.println(name);
//        }



        //4.创建一个人：Sql
        String sql2="SELECT * FROM user_data_tb WHERE telephone = '"+phone+"'";
        //5.创建一辆车：Statement
        Statement statement2 = conn.createStatement();
        //6.人上车，发车，走桥。
        ResultSet resultSet2 = statement2.executeQuery(sql2);

        List<UserDataBean> userDataBeanList = new ArrayList<>();
        while (resultSet2.next()) {
            String telephone = resultSet2.getString("telephone");
            String name = resultSet2.getString("name");
            String albumId = resultSet2.getString("albumId");
            String albumTitle = resultSet2.getString("albumTitle");
            String coverUrlLarge = resultSet2.getString("coverUrlLarge");
            String albumIntro = resultSet2.getString("albumIntro");
            String includeTrackCount = resultSet2.getString("includeTrackCount");
            String playCount = resultSet2.getString("playCount");
            String nickname = resultSet2.getString("nickname");

            UserDataBean userDataBean = new UserDataBean();
            userDataBean.setTelephone(telephone);
            userDataBean.setName(name);
            userDataBean.setId(Long.parseLong(albumId));
            userDataBean.setAlbumTitle(albumTitle);
            userDataBean.setCoverUrlLarge(coverUrlLarge);
            userDataBean.setAlbumIntro(albumIntro);
            userDataBean.setIncludeTrackCount(Long.parseLong(includeTrackCount));
            userDataBean.setPlayCount(Long.parseLong(playCount));
            userDataBean.setNickname(nickname);
            userDataBeanList.add(userDataBean);
        }

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        String s = gson.toJson(userDataBeanList);
        System.out.println(s);



        //7.炸车
//        statement.close();
        statement2.close();
        //8.炸桥
        conn.close();


        HttpServletResponse response = ServletActionContext.getResponse();
        response.setContentType("text/html");
        response.setCharacterEncoding("utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(s);
        writer.flush();
        return null;
    }

}
